<?php

	/**
	 * list the navigation for:
	 * a. frontend
	 * b. backend
	 * c. definied user_id
	 * 
	 * @package Yomm
	 * @author bsuter
	 */
	Class Yomm_Library_CmsNavigation {
		
		public $obj = null;
		
		public $nav = array();
		
		private $db = null;
		
		private $i18n = null;
		
		private $cat_id = null;
		
		private $list_for_user = 0;
		
		private $is_admin = 0;
		
		public $showAll = false;
		
		/**
		 * @param $user_id int check for this user
		 * @param $is_admin int 0 = no_admin; 1 = admin do display anything
		 */
		public function setUserAdmin($user_id, $is_admin)
		{
			$this->list_for_user = $user_id;
			$this->is_admin = $is_admin;
		}
		
		public function __construct($i18n = null, $cat_id = null, $showAll = false)
		{
			$this->showAll = $showAll;
			
			if(!isset($i18n) || is_null($i18n))
			{
				$i18n = SiteConfig::DefaultLanguage;
			}
			
			if(!isset($cat_id) || is_null($cat_id))
			{
				$cat_id = SiteConfig::DefaultCat;
			}
			
			$this->cat_id = $cat_id;
			
			$this->i18n = $i18n;
			
			$this->db = ydsql::getInstance();
		}
		
		public function getElements()
		{
			$this->list_elements(0,0);
			
			return $this->nav;
		}
		
		private function add_item($item, $layer)
		{
			if($layer == 0)
			{
				$this->nav[$item->id] = array(
					'id' => $item->id,
					'parent' => $item->parent,
					'link' => Tools::BuildLinkByParts($item->link, $item->i18n_tag, $item->cat),
					'rewrite' => $item->link,
					'title' => $item->title,
					'cat' => $item->cat,
					'status' => $item->status,
					'sub' => array()
				);
			}
			
			if($layer == 1)
			{
				$this->nav[$item->parent]['sub'][$item->id] = array(
					'id' => $item->id,
					'parent' => $item->parent,
					'link' => Tools::BuildLinkByParts($item->link, $item->i18n_tag, $item->cat),
					'rewrite' => $item->link,
					'title' => $item->title,
					'cat' => $item->cat,
					'status' => $item->status,
					'sub' => array()
				);
			}
			
			if($layer == 2)
			{
				$prev_obj = $this->db->select("cms_nav", array("parent"), "WHERE id='{$item->parent}'", false);
				
				$this->nav[$prev_obj->parent]['sub'][$item->parent]['sub'][$item->id] = array(
					'id' => $item->id,
					'parent' => $item->parent,
					'link' => Tools::BuildLinkByParts($item->link, $item->i18n_tag, $item->cat),
					'rewrite' => $item->link,
					'title' => $item->title,
					'status' => $item->status,
					'cat' => $item->cat
				);
			}
		}
		
		private function list_elements($parent, $layer)
		{
			$obj = $this->query($parent, 'object');
			
			foreach($obj as $key => $item)
			{
				$this->add_item($item, $layer);
				
				if($this->query($item->id, 'numrows') > 0)
				{
					$this->list_elements($item->id, $layer+1);
				}
			}
		}
		
		private function query($parent, $type)
		{
			if($this->showAll === false)
			{
				$option = "AND status=1";
			} else {
				$option = null;
			}
			
			if($this->list_for_user !== 0 && $this->is_admin == 0)
			{
				$user = "AND (cms_nav.id IN (SELECT admin_user_nav.cms_nav_id FROM admin_user_nav WHERE admin_user_nav.admin_user_id='{$this->list_for_user}') OR (cms_nav.user_id='{$this->list_for_user}'))";
			} else {
				$user = null;
			}
			
			if($parent == 0)
			{
				$where = "LEFT JOIN (cms_cat, cms_block LEFT JOIN (cms_language) ON (cms_block.language_id = cms_language.id)) ON (cms_nav.cat = cms_cat.id AND cms_nav.id = cms_block.nav_id) WHERE i18n = '{$this->i18n}' AND del=0 AND parent='{$parent}' AND cat='{$this->cat_id}' $option $user ORDER by cms_nav.sort ASC";
			} else {
				$where = "LEFT JOIN (cms_cat, cms_block LEFT JOIN (cms_language) ON (cms_block.language_id = cms_language.id)) ON (cms_nav.cat = cms_cat.id AND cms_nav.id = cms_block.nav_id) WHERE i18n = '{$this->i18n}' AND del=0 AND parent='{$parent}' $option $user ORDER by cms_nav.sort ASC";
			}
			
			$obj = $this->db->select("cms_nav", array(
				"parent",
				"cms_nav.id" => "id",
				"cms_block.rewrite_link" => "link",
				"cms_block.title" => "title",
				"cms_cat.rewrite_link" => "cat",
				"cms_nav.status" => "status",
				"cms_language.i18n" => "i18n_tag"
			), $where, true);

			if($type == 'object')
			{
				return $obj;
			}
			
			if($type == 'numrows')
			{
				return count($obj);
			}
		}
	}